package top.mistgc.mall.service.impl;

import com.fasterxml.jackson.databind.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import top.mistgc.mall.dto.UserLoginDTO;
import top.mistgc.mall.dto.UserRegisterDTO;
import top.mistgc.mall.entity.User;
import top.mistgc.mall.dao.UserDao;
import top.mistgc.mall.exception.CustomException;
import top.mistgc.mall.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import top.mistgc.mall.utils.JwtUtils;
import top.mistgc.mall.vo.UserLoginVO;

import javax.annotation.Resource;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2023-07-01 17:15:40
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Long id) {
        return this.userDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<User> queryByPage(User user, PageRequest pageRequest) {
        long total = this.userDao.count(user);
        return new PageImpl<>(this.userDao.queryAllByLimit(user, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.userDao.deleteById(id) > 0;
    }

    /**
     * @param loginDTO 
     * @return
     */
    @Override
    public UserLoginVO login(UserLoginDTO loginDTO) {
        User login = this.userDao.login(loginDTO.getUtell(), loginDTO.getUpwd());
        if (login == null) {
            throw new CustomException("Username or Password is wrong.");
        } else {
            UserLoginVO userLoginVO = new UserLoginVO();
            String token = JwtUtils.generateToken(login);
            BeanUtils.copyProperties(login, userLoginVO);
            userLoginVO.setToken(token);
            return userLoginVO;
        }
    }

    /**
     * @param utell
     * @return
     */
    @Override
    public User queryByUTell(String utell) {
        return this.userDao.queryByUTell(utell);
    }

    /**
     * @return 
     */
    @Override
    public Boolean register(UserRegisterDTO registerDTO) {
        // register logic
        User user = this.userDao.queryByUTell(registerDTO.getUtell());
        System.err.println(user);
        if (user == null) {
            // can register
            User user1 = new User();
            BeanUtils.copyProperties(registerDTO, user1);
            int insert = userDao.insert(user1);
            return insert > 0;
        } else {
            // can not register
//            throw new CustomException("User's phone number have be existed...");
            return false;
        }
    }
}
